package com.meitianhui.convenitenbuy.base;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.StatusBarUtil;

/**
 * Created by amy on 2017/11/3.
 */

public abstract class BaseToolbarActivity extends BaseActivity {

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private ImageView mTbBackAll;
    private TextView mTb_title;
    private ImageView mToolbarIvMenu;
    private TextView mToolbarTvMenu;
    private LinearLayout mToolbarMenu;
    private RelativeLayout mBaseToolBar;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_base_toolbar);
        setTranslucentStatus(R.color.white);
        StatusBarUtil.StatusBarLightMode(this);
        initID();
        initRxClick();
        setTbBackAllInVisible();
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onRefreshData();
            }
        });
    }

    protected void isEnable(boolean refresh) {
        mSwipeRefreshLayout.setEnabled(refresh);
    }

    /**
     * 设置刷新完成
     */
    protected void refreshFinish() {
        if (mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    protected void initRxClick() {
        RxUtils.bindEvents(mTbBackAll, this);
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.toolbar_back_all:
                clickBack();
                break;
            case R.id.toolbar_menu:
                clickMenu();
                break;
        }
    }

    /**
     * 菜单按钮的点击事件
     */
    protected void clickMenu() {
    }

    protected void clickBack() {
        finish();
    }

    protected void setToolBarGone() {
        mBaseToolBar.setVisibility(View.GONE);
    }

    protected void initID() {
        mSwipeRefreshLayout = getViewById(R.id.swipe_refresh_layout);
        mTbBackAll = getViewById(R.id.toolbar_back_all);
        mTb_title = getViewById(R.id.toolbar_title);
        mToolbarIvMenu = getViewById(R.id.toolbar_iv_menu);
        mToolbarTvMenu = getViewById(R.id.toolbar_tv_menu);
        mToolbarMenu = getViewById(R.id.toolbar_menu);
        mBaseToolBar = getViewById(R.id.base_tool_bar);
        FrameLayout mainContainer = (FrameLayout) findViewById(R.id.tb_container);
        LayoutInflater.from(this).inflate(setView(), mainContainer);
    }

    /**
     * 设置返回键不可见
     */
    protected void setTbBackAllInVisible() {
        if (isHideTbBack()) {
            mTbBackAll.setVisibility(View.INVISIBLE);
            mTbBackAll.setEnabled(false);
        }
    }

    /**
     * 设置菜单按钮可不可见
     * 文字 --> text为String
     * 图片 --> 返回R.mipmap.XXX即可
     */
    protected void setTbMenu(Object text) {
        if (text != null) {
            if (text instanceof String) {
                mToolbarIvMenu.setVisibility(View.INVISIBLE);
                mToolbarTvMenu.setVisibility(View.VISIBLE);
                mToolbarTvMenu.setText((String) text);
                RxUtils.bindEvents(mToolbarMenu, this);

            } else if (text instanceof Integer) {
                mToolbarTvMenu.setVisibility(View.INVISIBLE);
                mToolbarIvMenu.setVisibility(View.VISIBLE);
                mToolbarIvMenu.setImageResource((int) text);
                RxUtils.bindEvents(mToolbarMenu, this);
            }
        }
    }

    protected void setTbTitle(String title) {
        if (title != null) {
            mTb_title.setText(title);
        }
    }

    /**
     * setView
     *
     * @return resId
     */
    protected abstract int setView();

    /**
     * 返回按钮默认是不隐藏的
     */
    protected boolean isHideTbBack() {
        return false;
    }

    protected abstract void onRefreshData();

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

}
